package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DeviceInfoMapper;
import com.ruoyi.system.domain.DeviceInfo;
import com.ruoyi.system.service.IDeviceInfoService;

/**
 * 设备信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-15
 */
@Service
public class DeviceInfoServiceImpl implements IDeviceInfoService {
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询设备信息
     *
     * @param id 设备信息主键
     * @return 设备信息
     */
    @Override
    public DeviceInfo selectDeviceInfoById(Long id) {
        return deviceInfoMapper.selectDeviceInfoById(id);
    }

    /**
     * 查询设备信息列表
     *
     * @param deviceInfo 设备信息
     * @return 设备信息
     */
    @Override
    public List<DeviceInfo> selectDeviceInfoList(DeviceInfo deviceInfo) {
        if (deviceInfo.getVideoCount() == 0)
            deviceInfo.setVideoCount(-1);
        return deviceInfoMapper.selectDeviceInfoList(deviceInfo);
    }

    /**
     * 新增设备信息
     *
     * @param deviceInfo 设备信息
     * @return 结果
     */
    @Override
    public int insertDeviceInfo(DeviceInfo deviceInfo) {
        deviceInfo.setCreateTime(DateUtils.getNowDate());
        return deviceInfoMapper.insertDeviceInfo(deviceInfo);
    }

    /**
     * 修改设备信息
     *
     * @param deviceInfo 设备信息
     * @return 结果
     */
    @Override
    public int updateDeviceInfo(DeviceInfo deviceInfo) {
        deviceInfo.setUpdateTime(DateUtils.getNowDate());
        return deviceInfoMapper.updateDeviceInfo(deviceInfo);
    }

    /**
     * 批量删除设备信息
     *
     * @param ids 需要删除的设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceInfoByIds(Long[] ids) {
        return deviceInfoMapper.deleteDeviceInfoByIds(ids);
    }

    /**
     * 删除设备信息信息
     *
     * @param id 设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceInfoById(Long id) {
        return deviceInfoMapper.deleteDeviceInfoById(id);
    }

    @Override
    public List<DeviceInfo> getGroupList() {
        return deviceInfoMapper.getGroupList();
    }


    private static final String UPDATE_NURTURANCE = "device:deviceIds";

    @Override
    public int powerDevice(Long[] ids) {
        List<DeviceInfo> info = deviceInfoMapper.selectDeviceInfoByIds(ids);
//        String[] infoIds = info.deviceId
        String[] deviceIds = info.stream()
                .map(DeviceInfo::getDeviceId)
                .toArray(String[]::new);
//        String deviceId = info.getDeviceId();
        String jsonDeviceIds = JSON.toJSONString(deviceIds);
        redisCache.publish(UPDATE_NURTURANCE, jsonDeviceIds);
        return 1;
    }

    private static final String UPDATE_STOPPOWER = "device:stopPower";

    @Override
    public int stopPower(Long[] ids) {
        List<DeviceInfo> infoList = deviceInfoMapper.selectDeviceInfoByIds(ids);
        for (DeviceInfo info : infoList) {
            redisCache.setCacheObject(info.getDeviceId().replace(":", "-")+":status", 0);
        }

        return 1;
    }


    @Override
    public int pauseDevice(Long[] ids) {
        List<DeviceInfo> infoList = deviceInfoMapper.selectDeviceInfoByIds(ids);
        for (DeviceInfo info : infoList) {
            redisCache.setCacheObject(info.getDeviceId().replace(":", "-")+":status", 0);
        }

        return 1;
    }

    @Override
    public int pauseAllDevice() {
        List<DeviceInfo> infoList = deviceInfoMapper.selectAllDeviceInfo();
        for (DeviceInfo info : infoList) {
            redisCache.setCacheObject(info.getDeviceId().replace(":", "-")+":status", 0);
        }

        return 1;
    }
}
